var config = require('../../../config/config.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showPage: 2,
    minNum: 1,
    num: 1,
    maxNum: 1,
    rest_num: 0,
    index: 0,
    onExsit: false,
    selAddress: '',
    pageUrl: config.member_waterbill,
    waterbill_list: [],
    address_list: {
    },
    flag: 1,
    aid: 1,
    mid: 1,
    sid: 1,
    id: 0,
    buyer_name: '',
    buyer_address: '',
    mobile: '',
    place: 2,
  },
  //收货地址
  // toAddress: function (e) {
  //   var reback = 0;
  //   wx: wx.navigateTo({
  //     url: '../../member/address/address?reback=' + reback,
  //   })
  // },
  //没有任何水票去购买
  goTicket: function (event) {
    wx.switchTab({
      url: '../waterTicket/waterTicket',
    })
  },

  //调用接口
  getkeyUrl: function (pamars) {
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    var waterbill_list = that.data.waterbill_list;
    var index = that.data.index
    var arr
    wx.request({
      url: that.data.pageUrl,
      method: "GET",
      data: {
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
      },
      success: function (res) {
        if (res.data.data.waterbill_list != '') {
          var index = waterbill_list.length;
          var goods_list = res.data.data.waterbill_list;
          for (var i = 0; i < goods_list.length; i++) {
            waterbill_list[index + i] = goods_list[i];
          }
          that.setData({
            showPage: 0,
            waterbill_list: waterbill_list,
            rest_num: waterbill_list[0]['rest_num'],
            id: waterbill_list[0]['id'],
          })
        } else {
          that.setData({
            showPage: 1
          })
        }
      },
      fail: function (res) {
      }
    })
  },
  buttonTo: function () {
    wx.switchTab({
      url: '../../waterTicket/waterTicket',
    })
  },

  // 收货地址
  addAddress: function () {
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: config.address_index,
      method: "GET",
      data: {
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
      },
      success: function (res) {
        var result = res.data.data.info;
        if (result) {
          that.setData({
            place: 0,
            address_list: res.data.data.info,
            buyer_name: res.data.data.info.name,
            buyer_address: res.data.data.info.area_info + ' ' + res.data.data.info.address,
            mobile: res.data.data.info.mobile
          })
        } else {
          that.setData({
            place: 1
          })
        }
        if (!res.data.status) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },
  toAddress: function () {
    var reback = 0;
    wx: wx.navigateTo({
      url: '../../member/address/address?reback=' + reback,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var member_info = (wx.getStorageSync('member_info'));
    this.setData({
      mid: member_info.mid
    })

  },
  //相减
  toMinus: function () {
    var currentNum = this.data.num;
    if (currentNum > 1) {
      currentNum--;
    }
    this.setData({
      num: currentNum
    })
  },
  //相加
  toPlus: function () {
    var currentNum = this.data.num;
    if (currentNum >= this.data.rest_num) {
      return;
    }
    currentNum++;
    this.setData({
      num: currentNum
    })
  },
  //根据小程序的current属性，拿到index的值，绑定给每一个rest_num
  swiperChange: function (e) {
    var index = e.detail.current;
    this.setData({
      num: 1,
      rest_num: this.data.waterbill_list[index]['rest_num'],
      index: index,

    })

  },

  //点击图片，轮播图播放下一张
  swichNav: function (e) {
    var index = e.target.dataset.current;
    var length = this.data.waterbill_list.length - 1;
    if (index < 0) {
      index = length;
    }
    if (index > length) {
      index = 0;
    }
    this.setData({
      index: index
    });
  },
  //确认订水
  formSubmit: function (e) {
    var member_info = (wx.getStorageSync('member_info'));
    var mobile = member_info.mobile;
    var that = this;
    if (!mobile) {
      wx.showModal({
        title: '提示',
        content: "你还未填写个人信息,请填写",
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../../personal/personal',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return
    }
    wx.request({
      url: config.order_add_bill_order,
      method: 'POST',
      data: {
        id: this.data.id,
        num: this.data.num,
        mid: member_info.mid,
        aid: member_info.aid,
        sid: member_info.sid,
        buyer_name: this.data.buyer_name,
        mobile: this.data.mobile,
        buyer_address: this.data.buyer_address,
        description: e.detail.value.description,
        formId: e.detail.formId
      },
      success: function (res) {
        if (res.data.status) {
          wx.showModal({
            title: '提示',
            content: '你确定要订' + that.data.num + '桶水吗?',
            confirmColor: "#FD5019",
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: 'takeWater-success/takeWater-success',
                });
              }
            }
          });
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
          })
        }
      },
      fail: function (res) {
      },
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getkeyUrl();
    this.addAddress();
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (!currPage.data.address) {
      return
    }
    this.setData({//将携带的参数赋值
      address_list: currPage.data.address,
    });
    if (this.data.address_list) {
      this.setData({//将携带的参数赋值
        buyer_name: this.data.address_list.name,
        buyer_address: this.data.address_list.area_info + ' ' + this.data.address_list.address,
        mobile: this.data.address_list.mobile,
        palce: true,
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // wx.navigateBack({
    //   delta: 10
    // })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})